package com.fau.hm.action.index;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalRole;
import com.fau.hm.security.MySecurityMetadataSource;
import com.fau.hm.util.HttpRequestUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 角色信息维护Action
 * @author dangjh
 * @datetime 2014年10月14日,下午10:54:09
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class RoleAction extends PublicAction implements ModelDriven<MedicalRole> {

	private static final long serialVersionUID = 32007810302187786L;
	
	private MedicalRole role = new MedicalRole();
	
	@Resource
	private MySecurityMetadataSource mySecurityMetadataSource;
	
	@Resource
	private HttpRequestUtil httpRequestUtil;
	
	/**
	 * @desc 实例化角色列表页
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月12日,下午6:20:00
	 */
	@Action(value = "init_role_list", results = {@Result(name = SUCCESS, location = "init_role_list.jsp")})
	public String initroleList(){
		return SUCCESS;
	}

	/**
	 * @desc 角色数据列表展示
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:39:01
	 */
	@Action(value = "role_list_view")
	public String roleListView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getRoleService().findRoleList(map, role, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例角色信息添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_role_add", results = {@Result(name = SUCCESS, location = "medical_role_add.jsp")})
	public String roleAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存角色信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:29:51
	 */
	@Action(value = "medical_role_save")
	public String roleSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getRoleService().saveRole(role, admin);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 打开角色信息修改界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_role_edit", results = {@Result(name = SUCCESS, location = "medical_role_edit.jsp")})
	public String roleEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的角色信息 
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:30:54
	 */
	@Action(value = "medical_roleedit_save")
	public String roleEditSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(role.getId())){
			boolean flag = this.baseAllService.getRoleService().saveEditRole(role);
			if(flag){
				map.put("success", true);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 角色下是否包含用户
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:34:10
	 */
	@Action(value = "role_include_user")
	public String includeuser(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(role.getId())){
			boolean flag = this.baseAllService.getRoleService().includeUser(role.getId());
			if(flag){
				map.put("success", "true");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除角色
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:33:08
	 */
	@Action(value = "medical_role_del")
	public String roleDel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(role.getId())){
			this.baseAllService.getRoleService().delRole(role);
			map.put("success", "true");
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 实例化资源权限界面
	 * @return success
	 * @author dangjh
	 * @datetime 2014年11月27日 下午10:02:39
	 */
	@Action(value = "init_resource_list", results = {@Result(name = SUCCESS, location = "init_resource_list.jsp")})
	public String initResourceList() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载资源权限界面
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午10:04:22
	 */
	@Action(value = "init_resource_view")
	public String initResourceView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getRoleService().findAllResource();
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存分配的角色资源
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月29日 上午11:47:28
	 */
	@Action(value = "role_resource_save")
	public String saveRoleResource() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		String resourceIds = request.getParameter("resourceIds");
		if(StringUtils.isNotBlank(role.getId()) && StringUtils.isNotBlank(resourceIds)){
			boolean flag = this.baseAllService.getRoleService().saveRoleResource(role, resourceIds);
			if(flag){
				map.put("success", "true");
				mySecurityMetadataSource.flush();
				httpRequestUtil.refreshCmRole();
				httpRequestUtil.refreshOmRole();
				httpRequestUtil.refreshWmRole();
				httpRequestUtil.refreshFmRole();
				httpRequestUtil.refreshMmRole();
				httpRequestUtil.refreshSsRole();
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 加载角色对应的资源
	 * @return
	 * @author dangjh
	 * @datetime 2014年12月1日 下午5:22:07
	 */
	@Action(value = "role_resource_view")
	public String roleResourceView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getRoleService().findByRoleId(role.getId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 验证是否已存在当前添加的角色
	 * @return null
	 * @author dangjh
	 * 2014年12月24日 下午4:56:40
	 */
	@Action(value = "medical_include_role")
	public String includeRole() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getRoleService().includeRole(admin.getEnterpriseId(), role.getName());
		if(!flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalRole getModel() {
		return role;
	}

}
